package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.Service.BrandService;
import com.shop.Service.LogService;
import com.shop.Utils.TokenUtils;
import com.shop.common.TokenService;
import com.shop.pojo.Brand;
import com.shop.pojo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/api/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private LogService logService;
    @PostMapping("/addBrand")
    @ResponseBody
    public Message<String> addBrand(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String pname = map.get("pname").toString();
        String pimgurl = map.get("pimgurl").toString();
        Brand brand = new Brand();
        brand.setPname(pname);
        brand.setPimgurl(pimgurl);
        message = brandService.addBrand(brand);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,pname+"品牌的添加");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,pname+"品牌的添加");
        }
        message.setStatus("201");
        return message;
    }
    @PostMapping("/selectBrand")
    @ResponseBody
    public Message<PageInfo<Brand>> selectBrand(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<PageInfo<Brand>> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String queryInfo = ""+map.get("queryInfo");
        int pageSize =  Integer.parseInt(map.get("pageSize").toString());
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        message = brandService.selectAllBrand(queryInfo,pageSize,pageNum);
        message.setStatus("200");
        return message;
    }
    @PostMapping("/deleteBrand")
    @ResponseBody
    public Message<String> deleteBrand(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        int pid =  Integer.parseInt(map.get("pid").toString());
        message = brandService.deleteBrand(pid);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,pid+"品牌的删除");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,pid+"品牌的删除");
        }
        message.setStatus("201");
        return message;
    }
    @PostMapping("/updateBrand")
    @ResponseBody
    public Message<String> updateBrand(@RequestHeader(value = "Authorization",required = true) String Authorization,@RequestBody Map<String,Object> map) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        int pid =  Integer.parseInt(map.get("pid").toString());
        String pname = map.get("pname").toString();
        String pimgurl = map.get("pimgurl").toString();
        message = brandService.updateBrand(pid,pname,pimgurl);
        if (message.getData()==null){
            logService.addLog(tokenService.getUsername(Authorization),false,pname+"品牌的更新");
        }else {
            logService.addLog(tokenService.getUsername(Authorization),true,pname+"品牌的更新");
        }
        message.setStatus("201");
        return message;
    }
    @RequestMapping(value = "/uploadBrand",method = RequestMethod.POST)
    @ResponseBody
    public Message<String> upload(@RequestHeader(value = "Authorization",required = true) String Authorization,MultipartFile uploadFile) throws Exception {
        Message<String> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("D:\\upload\\Brand\\"+originalFilename));
        message.setStatus("201");
        message.setMessage("添加成功");
        message.setData(originalFilename);
        logService.addLog(tokenService.getUsername(Authorization),true,originalFilename+"品牌图片上传");
        return message;
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public Message<List<Brand>> selectAll(@RequestHeader(value = "Authorization",required = true) String Authorization) throws Exception{
        Message<List<Brand>> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        message = brandService.selectAll();
        message.setStatus("200");
        return message;
    }
    @RequestMapping("/selectCount")
    @ResponseBody
    public Message<Integer> selectCount(@RequestHeader(value = "Authorization",required = true) String Authorization) throws Exception{
        Message<Integer> message = new Message<>();
        if (TokenUtils.verify(Authorization) !=true){
            message.setStatus("401");
            message.setMessage("无权限");
            return message;
        }
        message = brandService.selectCount();
        message.setStatus("200");
        return message;
    }
    @PostMapping("/selectById")
    @ResponseBody
    public Message<Brand> selectById(@RequestBody Map<String,Object> map) throws Exception {
        Message<Brand> message =new Message<>();
        int pid =  Integer.parseInt(map.get("pid").toString());
        message = brandService.selectById(pid);
        message.setStatus("200");
        return message;
    }
}
